<template>
  <div class="role-list">
    <el-card>
      <div slot="header" class="clearfix">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="活动名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button class="middle-button" @click="dialogVisible = true">添加角色</el-button>
      <el-table
        :data="rolesData"
        style="width: 100%"
        v-loading="isLoading"
        border
        >
        <el-table-column
          prop="id"
          label="编号"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色名称"
        >
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
        >
        </el-table-column>
        <el-table-column
          prop="createdTime"
          label="添加时间"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="150px"
        >
          <template slot-scope="scope">
            <div>
            <el-button
              type="text"
              @click="$router.push({
                name: 'alloc-menu',
                params: {
                  roleId: scope.row.id
                }
              })"
            >分配菜单</el-button>
            <el-button
              type="text"
            >分配资源</el-button>
            </div>
            <div>
            <el-button
              size="mini"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              @click="handleDelete(scope.row)"
            >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="添加提示"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form ref="form" :model="formDialog" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formDialog.name"></el-input>
        </el-form-item>
        <el-form-item label="角色编码" prop="code">
          <el-input v-model="formDialog.code"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input type="textarea" v-model="formDialog.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleDialogSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加提示"
      :visible.sync="dialogVisible"
      width="30%">
      <add-edit
        :role-id="roleId"
      ></add-edit>
    </el-dialog>
  </div>
</template>

<script>
import addEdit from './addEdit'
import { getRolePages, deleteRoles, getSaveOrUpdate } from '@/services/role'
export default {
  name: 'role-list',
  components: {
    addEdit
  },
  data () {
    return {
      form: {
        name: ''
      },
      formDialog: {
        name: '',
        code: '',
        description: ''
      },
      dialogVisible: false,
      rolesData: [],
      isLoading: false,
      // 存储编辑的校色ID
      roleId: ''
    }
  },
  created () {
    this.loadRoles()
  },
  methods: {
    handleEdit (role) {
      this.dialogVisible = true
      this.roleId = role.id
    },
    async loadRoles () {
      this.isLoading = true
      const { data } = await getRolePages(this.form)
      // console.log(data)
      this.rolesData = data.data.records
      this.isLoading = false
    },
    handleDelete (role) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示')
        .then(async () => {
          await deleteRoles(role.id)
          this.loadRoles()
          // console.log(data)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async handleDialogSubmit () {
      const { data } = await getSaveOrUpdate(this.formDialog)
      if (data.code === '000000') {
        this.dialogVisible = false
        this.formDialog = {}
      }
    },
    onSubmit () {},
    onReset () {}
  }
}
</script>

<style lang="scss" scoped>
.middle-button{
  margin-bottom: 20px;
}
</style>
